import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import nprogress from 'nprogress' // @types/nprogress
import 'nprogress/nprogress.css'
import productRouter from './modules/product'
import Layout from '@/layout/index.vue'
// import { store } from '@/store/index'

const routes: RouteRecordRaw[] = [
  {
    path: '/Base1',
    name: 'Base1',
    // permissions: ['admin', 'visitor', 'vip'],
    // icon: '',
    component: () => import('../views/VDemo/VBase.vue'),
  },
  {
    path: '/',
    component: Layout,
    redirect: '/DemoForm',
    meta: {
      requiresAuth: true,
    },
    children: [
      {
        path: '/DemoTable',
        name: 'DemoTable',
        component: () => import('../views/VDemo/VDemoTable.vue'),
        meta: { title: '首页' },
      },
      {
        path: '/DemoForm',
        name: 'DemoForm',
        component: () => import('../views/VDemo/VDemoForm.vue'),
        meta: {
          title: 'DemoForm',
          isKeepAlive: true,
        },
      },
      {
        path: '/Base',
        name: 'Base',
        // permissions: ['admin', 'visitor', 'vip'],
        // icon: '',
        component: () => import('../views/VDemo/VBase.vue'),
      },
      {
        path: '/Tinymce',
        name: 'Tinymce',
        // permissions: ['admin', 'visitor', 'vip'],
        // icon: '',
        component: () => import('../views/VDemo/VDomeTinymce.vue'),
      },
      productRouter,
    ],
  },
  {
    // 404跳转到首页(此配置必须处于路由最后一项)
    path: '/:catchAll(.*)',
    redirect: { name: 'DemoForm' },
  },
]

const router = createRouter({
  history: createWebHashHistory(), // createWebHistory
  routes,
})

router.beforeEach((to, from) => {
  nprogress.start() // 开始加载进度条
  // if (to.meta.requiresAuth && !store.state.user) {
  //   // 此路由需要授权，请检查是否已登录
  //   // 如果没有，则重定向到登录页面
  //   return {
  //     path: '/admin/login',
  //     // 保存我们所在的位置，以便以后再来
  //     query: { redirect: to.fullPath }
  //   }
  // }
})

router.afterEach(() => {
  nprogress.done() // 加载进度条
})

export default router
